package logic

import (
	"context"
	"fmt"
	"shuqi-nove/models/cache"
	"shuqi-nove/models/mysql"

	"shuqi-nove/sqirpc/internal/svc"
	"shuqi-nove/sqirpc/sqirpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LonginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLonginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LonginLogic {
	return &LonginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LonginLogic) Longin(in *sqirpc.LonginRequest) (*sqirpc.LonginResponse, error) {
	// todo: add your logic here and delete this line

	get := cache.Rdb.Get(l.ctx, "sendSms"+in.Mobile)
	if get.Val() != in.SendSms {
		return nil, fmt.Errorf("手机号验证码获取失败")
	}
	var sqi mysql.Ass
	mysql.DB.Where("account =?", in.Account).Find(&sqi)
	if sqi.ID == 0 {
		return nil, fmt.Errorf("当前账号不存在,倩前去注册")
	}
	if sqi.Password != in.Password {
		return nil, fmt.Errorf("密码错误")
	}
	return &sqirpc.LonginResponse{
		Id: int64(sqi.ID),
	}, nil
}
